package com.ztjy.purchase.controller;

import com.github.pagehelper.PageInfo;
import com.ztjy.annonation.MyLog;
import com.ztjy.bean.CFirm;
import com.ztjy.bean.City;
import com.ztjy.purchase.biz.CFirmBiz;
import com.ztjy.sys.biz.AreaBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/purchase/manufacturer")
public class CFirmController {
    @Autowired
    private CFirmBiz fbiz;
    @Autowired
    private AreaBiz abiz;

    @ModelAttribute
    public void initModuleData(HttpSession session) {
        session.removeAttribute("mid");
        session.setAttribute("mid", 20);
    }

    @MyLog(value = "查询所有厂商管理操作")
    @RequestMapping("/flist")
    public String all(Model model,CFirm cFirm, @RequestParam(value = "pageno",defaultValue = "1")Integer pageNo){
        Integer pageSize=6;
        PageInfo<CFirm> cFirmPage = fbiz.findCFirmPage(cFirm, pageNo, pageSize);
        model.addAttribute("fpage",cFirmPage);
        model.addAttribute("firm",cFirm);
        return "forward:/purchase/manufacturer/manufacturerList.html";
    }
    @MyLog(value = "查询厂商详情操作")
    @RequestMapping("/Fdetails/{id}")
    public String details(@PathVariable("id")Integer firmId, HttpServletRequest request){
        CFirm byIdCFirm = fbiz.findByIdCFirm(firmId);
        request.setAttribute("frimd",byIdCFirm);
        return "forward:/purchase/manufacturer/manufacturerView";
    }
    @MyLog(value = "状态修改")
    @RequestMapping("/updateZt")
    public String zx(Integer firmId ,Model model,Integer fstatus ){
        CFirm cFirm = fbiz.findByIdCFirm(firmId);
        cFirm.setStatus(fstatus);
        boolean flag = fbiz.updateCFirm(cFirm);
        return "redirect:/purchase/manufacturer/flist";
    }

    @MyLog(value = "跳入修改")
    @RequestMapping("/goupdate/{id}")
    public String gouodate(@PathVariable("id")Integer firmId, Model model){
        CFirm byIdCFirm = fbiz.findByIdCFirm(firmId);
        model.addAttribute("fedit",byIdCFirm);
        List<City> cities = abiz.findAllCity();
        model.addAttribute("cities",cities);
        return "forward:/purchase/manufacturer/manufacturerUpdate";
    }
    @MyLog(value = "修改方法")
    @RequestMapping("doupdate")
    public String doupdate(CFirm firm){
        boolean flag = fbiz.updateCFirm(firm);
        if(flag){
            return "redirect:/purchase/manufacturer/flist";
        }else{
            return "forward:/purchase/manufacturer/manufacturerUpdate.html";
        }
    }
    @MyLog(value = "添加方法")
    @RequestMapping("/addFirm")
    public String add(CFirm firm){
        boolean flag = fbiz.addCFirm(firm);
        if(flag){
            return "redirect:/purchase/manufacturer/flist";
        }else{
            return "forward:/purchase/manufacturer/manufacturerAdd.html";
        }
    }

}
